// 四数相加 II
// Created by madison on 2022/11/4.
//

#include "vector"
#include "unordered_map"

using namespace std;

class Solution {
public:
    // 方法一：分组 + 哈希表
    int fourSumCount(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3, vector<int> &nums4) {
        unordered_map<int, int> countAB;
        for (int u: nums1) {
            for (int v: nums2) {
                ++countAB[u + v];
            }
        }
        int ans = 0;
        for (int u: nums1) {
            for (int v: nums2) {
                if (countAB.count(-u - v)) {
                    ans += countAB[-u - v];
                }
            }
        }
        return ans;
    }
};


